/*
 * 组织结构展示
 */
var url = '', jsonData = '';
$(function(){
	loadData();
});

function loadData(){
	loadTopCSS();
	/**
	 * 组织结构展示 树形图   树形表
	 */
	loadOrganizationStructureShow();
	
	/*loadTree();*/
};

function loadTopCSS() {
	$("#li1").addClass("navdown");
}


function loadTree(){
	
	url = ctx + '/organizationController/showTree';
	jsonData = {};
	var returnData= commonAjax();
	console.log(returnData);
	
	var width  = 500;
	var height = 500;
	
	var pack = d3.layout.pack()
	    			.size([ width, height ])
	    			.radius(20);
	
	var svg = d3.select("#treeMain").append("svg")
	    .attr("width", width)
	    .attr("height", height)
	    .append("g")
	    .attr("transform", "translate(0,0)");
	
	
	d3.json(url, function(error, root) {
		
		var nodes = pack.nodes(root);
		var links = pack.links(nodes);
		
		console.log(nodes);
		console.log(links);
		
		svg.selectAll("circle")
			.data(nodes)
			.enter()
			.append("circle")
			.attr("fill","rgb(31, 119, 180)")
			.attr("fill-opacity","0.4")
			.attr("cx",function(d){
				return d.x;
			})
			.attr("cy",function(d){
				return d.y;
			})
			.attr("r",function(d){
				return d.r;
			})
			.on("mouseover",function(d,i){
				d3.select(this)
					.attr("fill","yellow");
			})
			.on("mouseout",function(d,i){
				d3.select(this)
					.attr("fill","rgb(31, 119, 180)");
			});
		
		svg.selectAll("text")
					  .data(nodes)
					  .enter()
					  .append("text")
					  .attr("font-size","10px")
					  .attr("fill","white")
					  .attr("fill-opacity",function(d){
						  if(d.depth == 2)
							  return "0.9";
						  else
							  return "0";
					  })
					  .attr("x",function(d){ return d.x; })
					  .attr("y",function(d){ return d.y; })
					  .attr("dx",-12)
					  .attr("dy",1)
					  .text(function(d){ return d.name; });
		
	});
	
}


function loadOrganizationStructureShow() {
	url = ctx + '/organizationController/showOrganization';
	var departmentId = $('#departmentId').val();
	
	
	var paddNum = function(num){
        num += "";
        return num.replace(/^(\d)$/,"0$1");
      };
      
      if (1) {
		
		var date = new Date();
		var year = date.getFullYear();
		var month = paddNum(date.getMonth()+1);
		var day = paddNum(date.getDate());
		
		time = year+"-"+month+"-"+day;
		
      }
	
	
	jsonData = {departmentId:departmentId,date:time};
	
	var returnData= commonAjax();
	/*var map = [
	           {  name: 'BONC',
			        value: 6,
			        children: [
			                   {
				                name: '总经办',
				                value: 4,
				                children: [
				                    {
				                        name: '叶子节点1',
				                        value: 4
				                    }]
			                   },
			                   {
					                name: '商业服务',
					                value: 4,
					                children: [
					                    {
					                        name: '叶子节点1',
					                        value: 4
					                    }]
			                   },
			                   {
					                name: '研发',
					                value: 4
					                
				                   },
			           ]
            }
           ];
	
	initTreeChart(map,document.getElementById('organizationStructureShowMain'));*/
	console.info(returnData);
	initTreeChart(returnData,document.getElementById('organizationStructureShowMain'));
	//initForce(returnData,document.getElementById('organizationStructureShowMain'));
}

function commonAjax(){
	var result = '';
	$.ajax({
		type: 'GET',
		data: jsonData,
	    url: url,
	    async: false,
	    dataType:"json",
	    success: function(data){
	    	result = data;
	    },
	    error: function(){
	    }
	});
	return result;
}





/**
 * 树图 点击 刷新视图
 */
function loadOrganizationStructureClick(id){
	url = ctx + '/organizationController/showOrganization';
	var paddNum = function(num){
        num += "";
        return num.replace(/^(\d)$/,"0$1");
      };
      
      if (1) {
		
		var date = new Date();
		var year = date.getFullYear();
		var month = paddNum(date.getMonth()+1);
		var day = paddNum(date.getDate());
		
		time = year+"-"+month+"-"+day;
		
      }
	jsonData = {departmentId:id,date:time};
	console.log(jsonData);
	var returnData= commonAjax();
	initTreeChart(returnData,document.getElementById('organizationStructureShowMain'));
	
}
/**
 *  ！！！！！！！！！！！异步请求！！！！！！！！！！！！！
 */
/*function commonAjax(fun, dataType){
	$.ajax({
		type: 'POST',
		data: jsonData,
	    url: url,
	    dataType: dataType,
	    success: function(data){
	    	fun(data);
	    },
	    error: function(){
	    }
	});
}*/